import axios from 'axios'
import qs from 'qs'
import { Promise } from 'core-js'

// 创建一个axios实例
let instance = axios.create({
    baseURL: "http://localhost:2000",
    timeout: 5000,
    headers: {
        'content-type': 'application/x-www-form-urlencoded'
    }
})

// 编写请求拦截器
instance.interceptors.request.use(
    config => {
        config.method == 'post' ?
            config.data = qs.stringify({ ...config.data }) :
            config.params = { ...config.params };

        console.log(config);
        // config.headers['Content-Type'] = 'application/X-www-form-urlencoded';
        return config
    },
    err => {
        return Promise.reject(err)
    })

// 响应拦截器
instance.interceptors.response.use(
    response => {
        return response
    },
    error => {
        return Promise.reject(error)
    })

export default instance